﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Thorn : MonoBehaviour {

    [SerializeField, Tooltip("伤害值。包含伤害类型和伤害值默认1，激光伤害")]
    protected Damage physicalDamage = new Damage(1, null, DamageType.XRAY);
    // Use this for initialization
    [SerializeField, Tooltip("反弹系数")]
    private float bounceK=200;
    public float radus = 3f;
    public bool reverTurn = true;
    public void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Entity" || collision.gameObject.tag == "Player")
        {
            #region  受伤
            IVictim victim = collision.gameObject.GetComponent<IVictim>();
            Entity entity = collision.gameObject.GetComponent<Entity>();
            if(victim!=null)
            {
                physicalDamage.Victim = victim;//启动受伤
            }
            #endregion
            #region  反弹
            if (entity.Hp > 0)
            {
                //collision.
                Rigidbody hit = collision.transform.GetComponent<Rigidbody>();
                if (hit!=null)
                {
                    hit.AddExplosionForce(bounceK, transform.position, radus);
                }
                if (collision.gameObject.tag == "Player"&&reverTurn)
                {
                    MoveControl mc = collision.collider.GetComponent<MoveControl>();
                    StartCoroutine(Wait(mc));
                }
            }
#endregion
        }
    }
    private IEnumerator Wait(MoveControl mc)
    {
        int endDir = ((int)mc.dir + 2) % 4;
        int i = (int)mc.dir;
        while (i != endDir)
        {
            if (GameManager.Instance.Sui == null) break;
            if (i > endDir)
            {
                i -= 1;
            }
            else i = (i + 1) % 4;
            
                mc.Dir = (Direction)i;
            yield return null;
        }
    }//转方向
}